package com.metaera.app.ui.adapters;

import android.content.Context;
import android.widget.ImageView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.metaera.app.R;
import com.metaera.app.data.model.ArticleListResponse;
import com.metaera.app.ui.activities.ArticleDetailsActivity;
import com.metaera.app.utils.DateTime;
import com.metaera.app.utils.image.GlideApp;

import java.util.Date;

import androidx.annotation.NonNull;

public class HomeNewsAdapter extends BaseQuickAdapter<ArticleListResponse, BaseViewHolder> {

    public HomeNewsAdapter() {
        super(R.layout.item_quick_recommend_article);

        setOnItemClickListener((adapter, view, position) -> {
            Context context = view.getContext();
            context.startActivity(ArticleDetailsActivity.newInstance(context, getItem(position).getId()));
        });
    }

    @Override
    protected void convert(@NonNull BaseViewHolder holder, ArticleListResponse item) {
        holder.setText(R.id.tv_article_title, item.getTitle())
                .setText(R.id.tv_article_author_date, item.getAuthor().getNickname()
                        + " · "
                        + DateTime.format(new Date(item.getRelease_time_stamp() * 1000L)));

        ImageView ivArticleCover = holder.getView(R.id.iv_article_cover);
        GlideApp.with(ivArticleCover)
                .load(item.getPic())
                .miniThumb()
                .into(ivArticleCover);
    }

}
